//
//  FeedParser.m
//  look.book
//
//  Created by Chih Yuan Liu on 3/17/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FeedParser.h"
#import "TBXML.h"
#import "LBFeedItem.h"


@implementation FeedParser

-(NSArray*) parse:(NSString*)feeds{
	
	TBXML * tbxml = [[TBXML alloc] initWithXMLString:feeds];
// [[TBXML alloc] initWithURL:[NSURL URLWithString:@"http://lookbook.nu/rss"]];
	
	NSMutableArray * array= [[NSMutableArray alloc] init];
	
	TBXMLElement * root = tbxml.rootXMLElement;
	
	TBXMLElement * channel = [TBXML childElementNamed:@"channel" parentElement:root];
	
	if (channel) {
		
		TBXMLElement * item = [TBXML childElementNamed:@"item" parentElement:channel];
		
		while (item != nil) {
			
			LBFeedItem * lbitem = [[LBFeedItem alloc] init];
			
			TBXMLElement * title = [TBXML childElementNamed:@"title" parentElement:item];
			
			if (title != nil) {
				
				lbitem.title = [TBXML textForElement:title];
			}
			
			TBXMLElement * creator = [TBXML childElementNamed:@"dc:creator" parentElement:item];
			
			if (creator != nil) {
				
				lbitem.dcCreator = [TBXML textForElement:creator];
			}
			
			TBXMLElement * desc = [TBXML childElementNamed:@"description" parentElement:item];
			
			if (desc != nil) {
				
				lbitem.description = [TBXML textForElement:desc];
			}
			
			TBXMLElement * pdate = [TBXML childElementNamed:@"pubDate" parentElement:item];
			
			if (pdate != nil) {
				
				lbitem.pubDate = [TBXML textForElement:pdate];
			}
			
			TBXMLElement * imageSrc = [TBXML childElementNamed:@"imageSrc" parentElement:item];
			
			if (imageSrc != nil) {
				
				lbitem.imageSrc = [TBXML textForElement:imageSrc];
			}
			
			TBXMLElement * link = [TBXML childElementNamed:@"link" parentElement:item];
			
			if (link != nil) {
				
				lbitem.link = [TBXML textForElement:link];
			}
			
			
			[array addObject:lbitem];
			[lbitem release];
			
			item = [TBXML nextSiblingNamed:@"item" searchFromElement:item];
		}                      
	}
    [tbxml release];
	return array;
}

@end
